import 'dart:ui';

import 'package:flutter/material.dart';

class MyCustomPainterTest extends StatefulWidget {
  @override
  _MyCustomPainterTestState createState() => _MyCustomPainterTestState();
}

class _MyCustomPainterTestState extends State<MyCustomPainterTest>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('划线测试')),
      body: Center(
        child: CustomPaint(
          size: Size(200, 200),
          painter: MyCustomPainter(),
        ),
      ),
    );
  }
}

class MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var points = [
      Offset(0, size.height / 2),
      Offset(size.width / 3, size.height / 2),
      Offset(size.width * 2 / 3, size.height / 2),
      Offset(size.width, size.height / 2),
    ];
    // 划线
    canvas.drawPoints(
        PointMode.polygon,
        [Offset(0, size.height / 2), Offset(size.width / 3, size.height / 2)],
        Paint()
          ..color = Colors.red
          ..style = PaintingStyle.fill);
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(size.width / 3, size.height / 2),
          Offset(size.width * 2 / 3, size.height / 2)
        ],
        Paint()
          ..color = Colors.orange
          ..style = PaintingStyle.fill);
    canvas.drawPoints(
        PointMode.polygon,
        [
          Offset(size.width * 2 / 3, size.height / 2),
          Offset(size.width, size.height / 2),
        ],
        Paint()
          ..color = Colors.green
          ..style = PaintingStyle.fill);

    // 画圆
    canvas.drawCircle(
        Offset(0, size.height / 2),
        6,
        Paint()
          ..color = Colors.greenAccent
          ..style = PaintingStyle.fill);
    canvas.drawCircle(
        Offset(size.width / 3, size.height / 2),
        6,
        Paint()
          ..color = Colors.greenAccent
          ..style = PaintingStyle.fill);
    canvas.drawCircle(
        Offset(size.width * 2 / 3, size.height / 2),
        6,
        Paint()
          ..color = Colors.greenAccent
          ..style = PaintingStyle.fill);
    canvas.drawCircle(
        Offset(size.width, size.height / 2),
        6,
        Paint()
          ..color = Colors.greenAccent
          ..style = PaintingStyle.fill //绘画风格改为stroke
        );
  }

  @override
  bool shouldRepaint(MyCustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
